﻿using FastCloud.Base.Config;
using FastCloud.Base.Desgin;
using FastCloud.Base.Entity;
using FastIOC.Attribute;
using FastUtil.Convert;
using FastUtil.Data;
using System;
using System.Collections.Generic;
using System.Text;

namespace FastCloud.Base.DB
{
    public class DB_Group : BaseCacheML<Frame_Group>
    {
        public override void Insert(Frame_Group Entity)
        {
            if (string.IsNullOrEmpty(Entity.GroupGuid))
            {
                Entity.GroupGuid = GenerateUtil.GenerateRowGuid();
            }
            Entity.AddDate = DateTime.Now;
            base.Insert(Entity);
        }

        public override void InsertBatch(List<Frame_Group> EntityList)
        {
            foreach (Frame_Group Entity in EntityList)
            {
                if (string.IsNullOrEmpty(Entity.GroupGuid))
                {
                    Entity.GroupGuid = GenerateUtil.GenerateRowGuid();
                }
                Entity.AddDate = DateTime.Now;
            }
            base.InsertBatch(EntityList);
        }

        public Page SelectPage(string GroupName, string ParentGroupGuid)
        {
            int PageIndex = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageIndex"));
            int PageSize = ValueConvert.ConvertToInt32(CloudUtil.GetFormParam("PageSize"));
            List<Frame_Group> entityList = DTO.Select_Cache<Frame_Group>();
            int total = entityList.Count;
            if (!string.IsNullOrEmpty(GroupName))
            {
                entityList = entityList.FindAll((Frame_Group item) => item.GroupName.Contains(GroupName));
            }
            if (!string.IsNullOrEmpty(ParentGroupGuid) && ParentGroupGuid != "Top")
            {
                entityList = entityList.FindAll((Frame_Group item) => item.ParentGroupGuid == ParentGroupGuid);
            }
            entityList.Sort((x, y) => -x.SortNum.CompareTo(y.SortNum));
            List<Frame_Group> resultList = new List<Frame_Group>();
            for (int i = (PageIndex - 1) * PageSize; i < entityList.Count && i < PageIndex * PageSize; i++)
            {
                resultList.Add(entityList[i]);
            }
            Page page = new Page();
            page.Rows = resultList;
            page.Total = total;
            return page;
        }

        public List<Frame_Group> Select()
        {
            return DTO.Select_Cache<Frame_Group>(); ;
        }

        public bool ExistGroup(string GroupTag)
        {
            List<Frame_Group> entityList = DTO.Select_Cache<Frame_Group>();
            entityList = entityList.FindAll((Frame_Group item) => item.GroupTag == GroupTag);
            if (entityList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool ExistGroup(string GroupGuid,string GroupTag)
        {
            List<Frame_Group> entityList = DTO.Select_Cache<Frame_Group>();
            entityList = entityList.FindAll((Frame_Group item) => item.GroupTag == GroupTag && item.GroupGuid != GroupGuid);
            if (entityList.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
